﻿Imports Oracle.DataAccess.Client
Public Class fListaPrepack
    Dim daTipoPPack As lbGlobalcom.dsInventariosTableAdapters.INV_TIPOSPREPACKSTableAdapter
    Private lDaTipo As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Private lListaPPack As New List(Of lbGlobalcom.cPrepacksSelect)
    Public lIdPrepack As Integer
    Public lTipoPrepack As Integer
    Public lTipoSubProd As Integer
    Public lConsecutivo As Integer
    Public lProducto As String
    Public lPareja As String
    Public lBodega As String
    Public lNumeroRegP As String
    Private lTipo As Int16 = 3
    Private lEsLista As Boolean = False
    Public ReadOnly Property ListaPPack() As List(Of lbGlobalcom.cPrepacksSelect)
        Get
            Return lListaPPack
        End Get
    End Property
    Public Property EsLista() As Boolean
        Get
            Return lEsLista
        End Get
        Set(ByVal value As Boolean)
            lEsLista = value
        End Set
    End Property
    Public Property mTipo() As Int16
        Get
            Return lTipo
        End Get
        Set(ByVal value As Int16)
            lTipo = value
        End Set
    End Property
    Public Property mIdPrepack() As Integer
        Get
            Return lIdPrepack
        End Get
        Set(ByVal value As Integer)
            lIdPrepack = value
        End Set
    End Property
    Public Property mTipoPrepack() As Integer
        Get
            Return lTipoPrepack
        End Get
        Set(ByVal value As Integer)
            lTipoPrepack = value
        End Set
    End Property
    Public Property mTipoSubProd() As Integer
        Get
            Return lTipoSubProd
        End Get
        Set(ByVal value As Integer)
            lTipoSubProd = value
        End Set
    End Property
    Public Property mConsecutivo() As Integer
        Get
            Return lConsecutivo
        End Get
        Set(ByVal value As Integer)
            lConsecutivo = value
        End Set
    End Property
    Public Property mProducto() As String
        Get
            Return lProducto
        End Get
        Set(ByVal value As String)
            lProducto = value
        End Set
    End Property
    Public Property mPareja() As String
        Get
            Return lPareja
        End Get
        Set(ByVal value As String)
            lPareja = value
        End Set
    End Property

    Public Property mBodega() As String
        Get
            Return lBodega
        End Get
        Set(ByVal value As String)
            lBodega = value
        End Set
    End Property
    Public Property mNumeroRegP() As String
        Get
            Return lNumeroRegP
        End Get
        Set(ByVal value As String)
            lNumeroRegP = value
        End Set
    End Property

    Public Property ProductoPadre() As String
        Get
            Return txtProductoPadre.Text
        End Get
        Set(ByVal value As String)
            txtProductoPadre.Text = value
        End Set
    End Property
    Private Sub fListaPrepack_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If lEsLista Then
                Dim ColSel As New DataColumn("PPackSel", System.Type.GetType("System.Boolean"))
                ColSel.DefaultValue = False
                DsInventarios.INV_PREPACK.Columns.Add(ColSel)
                INV_PREPACKDataGridView.Columns("PPackSel").Visible = True
            Else
                INV_PREPACKDataGridView.Columns("PPackSel").Visible = False
            End If
            'lBodega = "001"
            lDaTipo = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            lDaTipo.FillByIdGrupo(DsGlobal.GLO_DATOSTABLAS, "sp")
            cbTipo.SelectedValue = lTipo
            daTipoPPack = New lbGlobalcom.dsInventariosTableAdapters.INV_TIPOSPREPACKSTableAdapter
            daTipoPPack.Fill(DsInventarios.INV_TIPOSPREPACKS)
            dtpFechaI.Value = Date.Now.AddDays(-120)
            dtpFechaF.Value = Date.Now
            cbTipoPrepack.SelectedValue = mIdPrepack
            If cbTipoPrepack.SelectedValue > 0 Then
                cbTipoPrepack.Enabled = False
            End If
            If lBodega IsNot Nothing Then
                txtBodega.Text = lBodega
                txtBodega.ReadOnly = True
                btnBuscaBodega.Enabled = False
            End If
            If lTipoSubProd = 1 Then
                txtProductoPadre.Text = lProducto
            ElseIf lTipoSubProd = 2 Then
                txtProductoHijo.Text = lProducto
            End If
            btnConsultar.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaProductoP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProductoP.Click
        Try
            Dim fLProd As New fListaProductos
            'fLProd.ListaPrecios = "001"
            If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                'dgvMovimiento.Rows(dgvMovimiento.CurrentRow.Index).Cells(1).Value = fLProd.idArticulo
                txtProductoPadre.Text = fLProd.idArticulo
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaProductoH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProductoH.Click
        Try
            Dim fLProd As New fListaProductos
            'fLProd.ListaPrecios = "001"
            If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                'dgvMovimiento.Rows(dgvMovimiento.CurrentRow.Index).Cells(1).Value = fLProd.idArticulo
                txtProductoHijo.Text = fLProd.idArticulo
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub ConsultaNoPrepacks()
        DsInventarios.INV_PREPACK.Rows.Clear()
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                Dim lWhere As String = "WHERE ESTADO = 'I' "
                Dim lSelect As String = "SELECT IDBODEGA, FECHAPREPACK,IDPRODUCTOP,TIPOSUBPROP,NUMEROREGISTROP,CONSECUTIVO CONSECPREPACK, PAREJA, '1' IDPREPACK, '0' IDTIPOPREPACK " & _
                                        "FROM INV_VDETALLE "
                Dim lOrder As String = "ORDER BY CONSECUTIVO, FECHAPREPACK "
                If txtBodega.Text.Length > 0 Then
                    lWhere &= " AND IDBODEGA='" & txtBodega.Text & "'"
                End If
                If txtProductoPadre.Text.Length > 0 Then
                    lWhere &= " AND IDPRODUCTOP='" & txtProductoPadre.Text & "'"
                End If
                If txtProductoHijo.Text.Length > 0 Then
                    lWhere &= " AND IDPRODUCTOP='" & txtProductoHijo.Text & "'"
                End If
                If dtpFechaI.Text.Length > 0 And dtpFechaF.Text.Length > 0 Then
                    lWhere &= " AND TRUNC(FECHAPREPACK) BETWEEN '" & dtpFechaI.Text & "' AND '" & dtpFechaF.Text & "'"
                End If
                If cbTipo.SelectedValue <> 3 Then
                    lWhere &= " AND TIPOSUBPROP='" & cbTipo.SelectedValue & "'"
                End If
                If txtNumeroReg.Text.Length > 0 Then
                    lWhere &= " AND NUMEROREGISTROP LIKE '%" & txtNumeroReg.Text & "%'"
                End If
                Try
                    cmd.CommandText = lSelect & " " & lWhere & " " & lOrder
                    conn.Open()
                    DsInventarios.INV_PREPACK.Load(cmd.ExecuteReader)
                    INV_PREPACKDataGridView.Focus()
                    INV_PREPACKDataGridView.Columns("IDPRODUCTOH").Visible = False
                    INV_PREPACKDataGridView.Columns("NUMEROREGISTROH").Visible = False
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Private Sub ConsultaPrePacks()
        DsInventarios.INV_PREPACK.Rows.Clear()
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim lWhere As String = " WHERE ESTADO='S'"
        Dim lSelect As String = "SELECT IDBODEGA,FECHAPREPACK,IDPRODUCTOP,TIPOSUBPROP,NUMEROREGISTROP" & _
                                     ",IDPRODUCTOH,TIPOSUBPRODH,NUMEROREGISTROH,CONSECPREPACK,IDPREPACK,IDTIPOPREPACK,PAREJA " & _
                                " FROM INV_VPREPACKS"
        Dim lOrder As String = " ORDER BY IDPREPACK,CONSECPREPACK,FECHAPREPACK"
        Try
            'cmd.Parameters.Add("PTIPOPREPACK", OracleDbType.Int32).Value = cbTipoPrepack.SelectedValue
            lWhere &= " AND IDTIPOPREPACK='" & cbTipoPrepack.SelectedValue & "'"
            If txtBodega.Text.Length > 0 Then
                lWhere &= " AND IDBODEGA='" & txtBodega.Text & "'"
            End If
            If txtProductoPadre.Text.Length > 0 Then
                lWhere &= " AND IDPRODUCTOP='" & txtProductoPadre.Text & "'"
            End If
            If txtProductoHijo.Text.Length > 0 Then
                lWhere &= " AND IDPRODUCTOH='" & txtProductoHijo.Text & "'"
            End If
            If dtpFechaI.Text.Length > 0 And dtpFechaF.Text.Length > 0 Then
                lWhere &= " AND TRUNC(FECHAPREPACK) BETWEEN '" & dtpFechaI.Text & "' AND '" & dtpFechaF.Text & "'"
            End If
            If cbTipo.SelectedValue <> 3 Then
                If cbTipo.SelectedValue = 1 Then
                    lWhere &= " AND TIPOSUBPROP='" & cbTipo.SelectedValue & "'"
                ElseIf cbTipo.SelectedValue = 2 Then
                    lWhere &= " AND TIPOSUBPRODH='" & cbTipo.SelectedValue & "'"
                End If
            End If
            If txtNumeroReg.Text.Length > 0 And cbTipo.SelectedValue <> 3 Then
                If cbTipo.SelectedValue = 1 Then
                    lWhere &= " AND NUMEROREGISTROP LIKE '%" & txtNumeroReg.Text & "%'"
                ElseIf cbTipo.SelectedValue = 2 Then
                    lWhere &= " AND NUMEROREGISTROH LIKE '%" & txtNumeroReg.Text & "%'"
                End If
            End If
            cmd.CommandText = lSelect & lWhere & lOrder
            conn.Open()
            DsInventarios.INV_PREPACK.Load(cmd.ExecuteReader)
            INV_PREPACKDataGridView.Focus()
            INV_PREPACKDataGridView.Columns("IDPRODUCTOH").Visible = True
            INV_PREPACKDataGridView.Columns("NUMEROREGISTROH").Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn.Close()
        End Try
    End Sub
    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        If cbTipoPrepack.SelectedValue = 0 Then
            ConsultaNoPrepacks()
        Else
            ConsultaPrePacks()
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub INV_PREPACKDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles INV_PREPACKDataGridView.CellMouseDoubleClick
        If Not lEsLista Then _
            AsignaDatos(e.RowIndex)
    End Sub

    Private Sub INV_PREPACKDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles INV_PREPACKDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            If Not lEsLista Then _
                AsignaDatos(INV_PREPACKDataGridView.CurrentRow.Index)
        End If
    End Sub

    Private Sub AsignaDatos(ByVal Fila As Integer)
        If Fila >= 0 And Not lEsLista Then
            lIdPrepack = INV_PREPACKDataGridView.Rows(Fila).Cells("IDPREPACK").Value
            lProducto = INV_PREPACKDataGridView.Rows(Fila).Cells("IDPRODUCTOP").Value
            lConsecutivo = INV_PREPACKDataGridView.Rows(Fila).Cells("CONSECUTIVO").Value
            lTipoPrepack = INV_PREPACKDataGridView.Rows(Fila).Cells("IDTIPOPREPACK").Value
            lTipoSubProd = INV_PREPACKDataGridView.Rows(Fila).Cells("TIPOSUBPROP").Value
            lPareja = INV_PREPACKDataGridView.Rows(Fila).Cells("PAREJA").Value
            lNumeroRegP = INV_PREPACKDataGridView.Rows(Fila).Cells("NUMEROREGISTROP").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub


    Private Sub btnBuscaBodega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaBodega.Click
        Try
            Dim flBod As New fListaBodegas
            If flBod.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtBodega.Text = flBod.Bodega
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        Try
            If lEsLista Then
                For Each dr As lbGlobalcom.dsInventarios.INV_PREPACKRow In DsInventarios.INV_PREPACK.Rows
                    If dr("PPackSel") Then
                        Dim cSelect As New lbGlobalcom.cPrepacksSelect
                        cSelect.IdPrepack = dr.IDPREPACK
                        cSelect.Producto = dr.IDPRODUCTOP
                        cSelect.Consecutivo = dr.CONSECPREPACK
                        cSelect.TipoPreapck = dr.IDTIPOPREPACK
                        cSelect.TipoSubProd = dr.TIPOSUBPROP
                        cSelect.Pareja = dr.PAREJA
                        cSelect.NumeroRegP = dr.NUMEROREGISTROP
                        lListaPPack.Add(cSelect)
                        cSelect = Nothing
                    End If
                Next
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Else
                AsignaDatos(INV_PREPACKDataGridView.CurrentRow.Index)
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class